﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core;
using Microhard.Core.DBUtility;
using Microhard.Plat.CRM.Customers.Interface;
using Microhard.Plat.CRM.Customers.DataAccess;
using Microhard.Plat.CRM.Customers.Models;
using Microhard.Plat.CRM.Customers.DTO;


namespace Microhard.Plat.CRM.Customers.Impl
{
    public class OrderImpl : IOrder
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.OrderModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.OrderModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.OrderModel model)
        {
            throw new NotImplementedException();
        }

        public Models.OrderModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.OrderModel GetModelByModel(Models.OrderModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderModel> Query(Models.OrderModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            IEnumerable<Order> entities = null;
            totalCount = 0;
            entities = from b in db.Orders
                       where b.CompanyID == this.scope.EmpInfo.CompanyID
                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<OrderModel>();
        }

        public List<Models.OrderModel> Query(Models.OrderModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }


        public void UpdateReceiveGoods(Guid companyID, List<OrderModel> orders)
        {
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var rcCodes = orders.Select(o => o.Code).ToList();
            var extOrders = (from o in db.Orders
                             where rcCodes.Contains(o.Code)
                             && o.CompanyID == companyID
                             select o).ToList();
            if (extOrders.Count > 0)
            {
                foreach (var o in extOrders)
                {
                    o.DeliveryDate = DateTime.Now;
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error("UpdateReceiveGoods", ex);
                }
            }
        }

    }
}
